package com.crape.account.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.crape.account.bo.IAccountBO;
import com.crape.account.bo.IDarrenIndexBO;
import com.crape.common.bean.LoginUser;
import com.crape.common.enums.ArticleIndexType;
import com.crape.common.enums.DarrenIndexType;
import com.crape.common.model.AccountInfo;
import com.crape.common.model.Article;
import com.crape.common.model.Question;
import com.crape.common.model.Subject;
import com.crape.faq.bo.IQuestionBO;
import com.crape.topic.bo.IArticleBO;
import com.crape.topic.bo.IArticleIndexBO;
import com.crape.topic.bo.ISubjectBO;

@Controller
public class HomeController {

	private IAccountBO accountBOImpl;
	private ISubjectBO subjectBOImpl;
	private IArticleBO articleBOImpl;
	private IArticleIndexBO articleIndexBOImpl;
	private IDarrenIndexBO darrenIndexBOImpl;
	private IQuestionBO questionBOImpl;

	@Autowired
	public void setAccountBOImpl(IAccountBO accountBOImpl) {
		this.accountBOImpl = accountBOImpl;
	}

	@Autowired
	public void setSubjectBOImpl(ISubjectBO subjectBOImpl) {
		this.subjectBOImpl = subjectBOImpl;
	}

	@Autowired
	public void setArticleBOImpl(IArticleBO articleBOImpl) {
		this.articleBOImpl = articleBOImpl;
	}

	@Autowired
	public void setArticleIndexBOImpl(IArticleIndexBO articleIndexBOImpl) {
		this.articleIndexBOImpl = articleIndexBOImpl;
	}

	@Autowired
	public void setDarrenIndexBOImpl(IDarrenIndexBO darrenIndexBOImpl) {
		this.darrenIndexBOImpl = darrenIndexBOImpl;
	}

	@Autowired
	public void setQuestionBOImpl(IQuestionBO questionBOImpl) {
		this.questionBOImpl = questionBOImpl;
	}

	@RequestMapping("/home.jsp")
	public String home(HttpServletRequest request, ModelMap modelMap) {
		HttpSession session = request.getSession();
		List<Subject> Subjects = subjectBOImpl.listSubjects(new Subject());
		session.setAttribute("topSubjects", Subjects);
		LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_LOGINUSER);
		if (loginUser != null) {
			AccountInfo account = accountBOImpl.getAccountInfoById(loginUser.getAccountid());
			modelMap.put("account", account);
		}
		List<Article> flashArticles = articleIndexBOImpl.getIndexShowArticles(ArticleIndexType.flash.getCode(), 6);
		if(flashArticles==null||flashArticles.size()<6){
			List<Article> list1  = articleBOImpl.getAllArticles(1, 6);
			flashArticles.addAll(list1);
		}
		List<Article> leftArticles = articleIndexBOImpl.getIndexShowArticles(ArticleIndexType.left.getCode(), 3);
		if(leftArticles==null||leftArticles.size()<3){
			List<Article> list1  = articleBOImpl.getAllArticles(1, 3);
			leftArticles.addAll(list1);
		}
		List<Article> rightArticles = articleIndexBOImpl.getIndexShowArticles(ArticleIndexType.right.getCode(), 3);
		if(rightArticles==null||rightArticles.size()<3){
			List<Article> list1  = articleBOImpl.getAllArticles(1, 3);
			rightArticles.addAll(list1);
		}
		modelMap.put("flashArticles", flashArticles);
		modelMap.put("leftArticles", leftArticles);
		modelMap.put("rightArticles", rightArticles);
		List<AccountInfo> leftDarrens = darrenIndexBOImpl.getIndexShowDarrens(DarrenIndexType.left.getCode(), 2);
		if(leftDarrens==null||leftDarrens.size()<3){
			List<AccountInfo> list1 = accountBOImpl.getAllDarrens(1, 2);
			leftDarrens.addAll(list1);
		}
		List<AccountInfo> rightDarrens = darrenIndexBOImpl.getIndexShowDarrens(DarrenIndexType.right.getCode(), 4);
		if(rightDarrens==null||rightDarrens.size()<3){
			List<AccountInfo> list1 = accountBOImpl.getAllDarrens(1, 2);
			rightDarrens.addAll(list1);
		}
		modelMap.put("leftDarrens", leftDarrens);
		modelMap.put("rightDarrens", rightDarrens);
		List<Question> hotQuestions = questionBOImpl.getHotQuestions();
		modelMap.put("hotQuestions", hotQuestions);
		return "/home.html";
	}
}
